<template>
  <layout-body>
    <div>
      <el-button
        type="primary"
        @click="createClick(0)"
        v-permission="'system-config_tab-create'"
      >
        <el-icon><Plus /></el-icon>
        添加配置分类
      </el-button>
    </div>
    <!-- 列表 -->
    <el-table
      v-loading="initLoading"
      class="mt20"
      :data="lists"
      row-key="id"
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column label="分类名称" prop="title">
        <template #default="scope">
          <span>{{ scope.row.html }}</span>
          <span>{{ scope.row.title }}</span>
        </template></el-table-column
      >
      <el-table-column label="分类字段标识" prop="eng_field">
        <template #default="scope">
          <span>{{ scope.row.eng_field }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status">
        <template #default="scope">
          <el-switch
            v-model="scope.row.status.value"
            size="small"
            :active-value="1"
            :inactive-value="0"
            :loading="scope.row.loading"
            :before-change="
              () => {
                return (scope.row.switch = true);
              }
            "
            @change="statusChange($event, scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column align="left" label="操作" width="200">
        <template #default="scope">
          <el-button link type="primary" @click="goList(scope.row.id)"
            >配置列表</el-button
          >
          <el-button
            link
            type="primary"
            @click="createClick(scope.row.id)"
            v-permission="'system-config_tab-update'"
            >编辑</el-button
          >

          <el-popconfirm
            confirm-button-text="确定"
            cancel-button-text="不用了"
            icon="WarningFilled"
            icon-color="red"
            title="确定删除吗？"
            @confirm="del(scope.row.id)"
          >
            <template #reference>
              <el-button
                link
                type="primary"
                v-permission="'system-config_tab-delete'"
                >删除</el-button
              >
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加 -->
    <systemConfigTabCreate
      ref="createComponentRef"
      @success="toInit()"
    ></systemConfigTabCreate>
  </layout-body>
</template>
<script setup lang="ts">
import { ref, onMounted, getCurrentInstance } from "vue";
import { useRouter } from "vue-router";
import systemConfigTabCreate from "./create.vue";
import {
  getConfigTabList,
  updateStatusConfigTab,
  delConfigTab,
} from "@/api/system/config";
import { Result, ResultError } from "@/types";

const router = useRouter();

const { proxy } = getCurrentInstance() as any;
const {
  proxy: { $utils },
} = getCurrentInstance() as any;

const createComponentRef = ref<HTMLElement>();

const initLoading = ref(true);
const lists = ref([]);
const toInit = () => {
  initLoading.value = true;
  getConfigTabList({})
    .then((res: Result) => {
      lists.value = res.data;
      setTimeout(() => {
        initLoading.value = false;
      }, 300);
    })
    .catch((err: ResultError) => {
      $utils.errorMsg(err);
    });
};

const createClick = (id: number | string) => {
  proxy?.$refs["createComponentRef"]?.open(id);
};

const statusChange = (
  val: number,
  row: {
    switch: boolean;
    loading: boolean;
    id: number;
  }
) => {
  if (row.switch === true) {
    row.loading = true;
    updateStatusConfigTab({
      id: row.id,
      status: val,
    })
      .then((res: Result) => {
        row.loading = false;
        toInit();
        $utils.successMsg(res);
      })
      .catch((err: ResultError) => {
        row.loading = false;
        toInit();
        $utils.errorMsg(err);
      });
  }
};

const del = (id: number) => {
  delConfigTab({
    id,
  })
    .then((res: Result) => {
      toInit();
      $utils.successMsg(res.message);
    })
    .catch((err: ResultError) => {
      $utils.errorMsg(err);
    });
};

// 跳转到配置列表页面
const goList = (id: any) => {
  router.push({
    path: "/system/config/list",
    query: {
      tab_id: id,
    },
  });
};

onMounted(() => {
  toInit();
});
</script>